package com.water.camera.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.water.camera.adapter.PinImgAdapter;
import com.water.camera.adapter.PinStyle2Adapter;
import com.water.camera.adapter.PinStyleAdapter;
import com.water.camera.adapter.PinTabAdapter;
import com.water.camera.adapter.PinWordAdapter;
import com.water.camera.bean.EditItem;
import com.water.camera.bean.MediaData;
import com.water.camera.databinding.ActivityDmImageJigsawBinding;
import com.water.camera.fragment.AddWord2Dlg;
import com.water.camera.fragment.BaseDialogFragment;
import com.water.camera.fragment.EditPicDlg;
import com.water.camera.mark.BaseMark;
import com.water.camera.util.Read;
import com.water.camera.util.SaveUtils;
import com.water.camera.util.ToastUtil;
import com.water.camera.util.Utils;
import com.water.camera.view.temp.Temp1View;
import com.water.camera.view.temp.Temp2View;
import com.water.camera.view.temp.Temp3View;
import com.water.camera.view.temp.Temp4View;

import java.util.ArrayList;

public class PinAct extends BaseActivity {

    ActivityDmImageJigsawBinding binding;
    BaseMark mMark;
    PinImgAdapter adapter;
    int scroll;
    PinWordAdapter wordAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDmImageJigsawBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.ivBack.setOnClickListener(v -> finish());
        binding.vpMenu.setAdapter(new PinTabAdapter(getSupportFragmentManager()));
        binding.tabMenu.setViewPager(binding.vpMenu);
        binding.tabMenu.setOnScrollChangeListener(new SmartTabLayout.OnScrollChangeListener() {
            @Override
            public void onScrollChanged(int scrollX, int oldScrollX) {
                scroll = scrollX;
            }
        });
        binding.tabMenu.setOnTabClickListener(new SmartTabLayout.OnTabClickListener() {
            @Override
            public void onTabClicked(int position) {
                if (position == 0) {
                    binding.rvStyle.setVisibility(View.VISIBLE);
                    binding.rvStyleLayout.setVisibility(View.INVISIBLE);
                } else {
                    binding.rvStyle.setVisibility(View.INVISIBLE);
                    binding.rvStyleLayout.setVisibility(View.VISIBLE);
                }
            }
        });
        binding.tabMenu.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP: {
                        int w = binding.tabMenu.getChildAt(0).getWidth();
                        if (scroll < w / 4) {
                            binding.tabMenu.scrollTo(0, 0);
                            binding.rvStyle.setVisibility(View.VISIBLE);
                            binding.rvStyleLayout.setVisibility(View.INVISIBLE);
                        } else {
                            binding.tabMenu.scrollTo(w / 2, 0);
                            binding.rvStyle.setVisibility(View.INVISIBLE);
                            binding.rvStyleLayout.setVisibility(View.VISIBLE);
                        }
                        break;
                    }
                }
                return false;
            }
        });

        binding.rvStyle.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        binding.rvStyle.setAdapter(new PinStyleAdapter(this));

        binding.rvStyleLayout.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        binding.rvStyleLayout.setAdapter(new PinStyle2Adapter(this));

        binding.imageRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PinImgAdapter(this);
        binding.imageRv.setAdapter(adapter);

        binding.wordRv.setLayoutManager(new LinearLayoutManager(this));
        wordAdapter = new PinWordAdapter(this);
        binding.wordRv.setAdapter(wordAdapter);

        binding.tvAddImg.setOnClickListener(v -> {
            Read.toRead(this, new Runnable() {
                @Override
                public void run() {
                    Intent i = new Intent(PinAct.this, BatchPhotoAct.class);
                    i.putExtra("from", 1);
                    startActivityForResult(i, 1001);
                }
            });
        });
        binding.tvText.setOnClickListener(v -> {
            addWord(null, -1);
        });

        onTempChange(0);

        binding.tvRight.setOnClickListener(v -> {
            save();
        });
    }

    private void save() {
        showLoading();
        adapter.showSelect(false);
        h.postDelayed(() -> {
            Bitmap bm = Utils.convertViewToBitmap(binding.contstantTop);
            SaveUtils.saveBitmapToAlbum(this, bm);
            ToastUtil.showShortToast(this, "已保存");
            hideLoading();
            finish();
        }, 500);
    }

    public void toEdit(EditItem item, int position) {
        addWord(item, position);
    }

    private void addWord(EditItem item, int position) {
        AddWord2Dlg dlg = new AddWord2Dlg().setKey(null, item);
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {
                if (isOk) {
                    if (position == -1) {
                        wordAdapter.append((EditItem) value);
                    } else {
                        if (value == null) {
                            wordAdapter.notifyItemRemoved(position);
                        } else {
                            wordAdapter.update((EditItem) value, position);
                        }
                    }
                }
            }
        });
        dlg.show(getSupportFragmentManager(), "");
    }

    public void onTempChange(int i) {
        binding.rlTop.removeAllViews();
        switch (i) {
            case 0:
                mMark = new Temp1View(this);
                break;
            case 1:
                mMark = new Temp2View(this);
                break;
            case 2:
                mMark = new Temp3View(this);
                break;
            default:
                mMark = new Temp4View(this);
        }
        binding.rlTop.addView(mMark);
    }

    public void onTempLayoutChange(int i) {
        switch (i) {
            case 0:
                binding.imageRv.setLayoutManager(new LinearLayoutManager(this));
                break;
            case 1:
                binding.imageRv.setLayoutManager(new GridLayoutManager(this, 2));
                break;
            case 2:
                binding.imageRv.setLayoutManager(new GridLayoutManager(this, 3));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10) {//编辑
            if (resultCode == RESULT_OK) {
                mMark.update();
            }
        } else if (requestCode == 1001) {//选图
            if (resultCode == RESULT_OK) {
                ArrayList<MediaData> list = data.getParcelableArrayListExtra("img");
                adapter.append(list);
            }
        } else if (requestCode == 1002) {//选图
            if (resultCode == RESULT_OK) {
                int p = data.getIntExtra("position", -1);
                if (p < 0) {
                    return;
                }
                ArrayList<MediaData> list = data.getParcelableArrayListExtra("img");
                if (list.isEmpty()) {
                    return;
                }
                adapter.insert(p, list);
            }
        }
    }

    public void editPic(int p, MediaData item) {
        EditPicDlg dlg = new EditPicDlg();
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {
                if (isOk) {
                    int i = (Integer) value;
                    if (i == 0) {
                        Intent in = new Intent(PinAct.this, BatchPhotoAct.class);
                        in.putExtra("from", 1);
                        in.putExtra("position", p);
                        startActivityForResult(in, 1002);
                    } else {
                        adapter.remove(p);
                    }
                }
            }
        });
        dlg.show(getSupportFragmentManager(), "");
    }

    @Override
    protected boolean paddingTop() {
        return false;
    }
}
